
package com.admissions.utils;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.admissions.R;
import com.admissions.common.Constants;
import com.admissions.model.CauHoi;
import com.admissions.model.DapAn;
import com.admissions.model.DeThi;
import com.admissions.model.MonThi;

/**
 * @author Bkav-DangNVb
 *
 */
public class JSONHelper implements Constants {

    public static ArrayList<CauHoi> getAllQuestions(String jsonString) {
        ArrayList<CauHoi> questions = new ArrayList<CauHoi>();
        try {
            JSONObject json = new JSONObject(jsonString);
            JSONObject response = json.getJSONObject(RESPONSE);
            String status = response.getString(STATUS);

            if (RESULT_TRUE.equals(status)) {
                JSONObject result = response.getJSONObject(RESULT);
                JSONArray jsonArray = result.getJSONArray(CauHoi.CAUHOI);
                int length = jsonArray.length();

                for (int i = 0; i < length; i++) {
                    JSONObject js = jsonArray.getJSONObject(i);
                    String item = js.getString(CauHoi.ID);
                    String content = js.getString(CauHoi.CAUHOI);
                    String dapanStr = js.getString(DapAn.DAPAN);
                    String[] dapansArray = dapanStr.split("<dapan>");

                    ArrayList<DapAn> dapans = new ArrayList<DapAn>();
                    for (int j = 0; j < dapansArray.length; j++) {
                        DapAn da = new DapAn(dapansArray[i]);
                        dapans.add(da);
                    }

                    CauHoi cauhoi = new CauHoi(item, content, dapans);
                    questions.add(cauhoi);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {

        }
        Log.e("questions", questions.toString());
        return questions;
    }

    public static ArrayList<DeThi> getAllSections(Context context, String jsonString) {
        ArrayList<DeThi> sections = new ArrayList<DeThi>();
        try {
            JSONObject json = new JSONObject(jsonString);
            JSONObject response = json.getJSONObject(RESPONSE);
            String status = response.getString(STATUS);

            if (RESULT_TRUE.equals(status)) {
                JSONObject result = response.getJSONObject(RESULT);
                JSONArray jsonArray = result.getJSONArray(DeThi.DETHI);
                int length = jsonArray.length();

                for (int i = 0; i < length; i++) {
                    JSONObject js = jsonArray.getJSONObject(i);
                    String id = js.getString(MonThi.ID);
                    String name = js.getString(MonThi.NAME);
                    String description = js.getString(MonThi.DESCRIPTION);

                    /*String icon_link = js.getString(Subject.ICON);
                    ImageLoader loader = new ImageLoader(context);
                    Bitmap icon = loader.getBitmap(icon_link);*/

                    Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.icon);

                    DeThi dethi = new DeThi(icon, id, name, description);

                    sections.add(dethi);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {

        }
        return sections;
    }

    /*  public static ArrayList<MonThi> getAllSubjects(Context context, String jsonString) {
          ArrayList<MonThi> monThis = new ArrayList<MonThi>();
          JSONObject json = null;
          try {
              json = new JSONObject(jsonString);
          } catch (JSONException e) {
              Log.e("Error Create Json Object", e.toString());
          }

          try {
              JSONArray jsonArray = json.getJSONArray(MonThi.INDENTIFICATION);
              for (int i = 0; i < jsonArray.length(); i++) {
                  JSONObject js = jsonArray.getJSONObject(i);
                  String id = js.getString(MonThi.ID);
                  String name = js.getString(MonThi.NAME);
                  String description = js.getString(MonThi.DESCRIPTION);
                  String icon_link = js.getString(Subject.ICON);
                  ImageLoader loader = new ImageLoader(context);
                  Bitmap icon = loader.getBitmap(icon_link);

                  Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);

                  MonThi category = new MonThi(icon, id, name, description);

                  monThis.add(category);
              }
          } catch (Exception e) {
              Log.e("Error Parsing Json", e.toString());
          }
          return monThis;
      }*/

}
